import pandas as pd
from area import Area

def rectangle2set(floor, minx, miny, maxx, maxy):
    rect = set()
    for x in range(minx, maxx + 1):
        for y in range(miny, maxy + 1):
            rect.add((floor, x,y))
    return rect


def f_pos2sensor(sensor:dict, pos:set):
    ret = set()
    for p in pos:
        if sensor.get(p) is not None:
            ret.add(sensor[p])
    return ret


def mapping():
    org_distri = pd.read_csv('../../dataset/org/distribution/distribution.csv', sep=',')
    org_distri = org_distri.to_numpy()
    
    sensor2pos = {}

    sensor2area = {}

    pos2sensor = {}
    
    for [sid, fl, x, y] in org_distri:
        if fl == 1:
            sensor2pos[sid] = (fl,x,y)
            pos2sensor[(fl, x,y)] = sid
        elif fl==2:
            sensor2pos[sid] = (fl,x,y)
            pos2sensor[(fl,x,y)] = sid

    f1={}
    f1['sub_venue_A'] = rectangle2set(1, 2, 1, 3, 5)
    f1['sub_venue_B'] = rectangle2set(1, 4, 1, 5, 5)
    f1['sub_venue_C'] = rectangle2set(1, 6, 1, 7, 5)
    f1['sub_venue_D'] = rectangle2set(1, 8, 1, 9, 5)
    f1['poster_hall'] = rectangle2set(1, 3, 7, 9, 8)
    f1['toilet1'] = rectangle2set(1, 4, 10, 5, 11)
    f1['room1'] = rectangle2set(1, 6, 10, 9, 11)
    f1['room2'] = rectangle2set(1, 10, 10, 11, 11)
    f1['register'] = rectangle2set(1, 12, 2, 13, 5)
    f1['exhibition_hall'] = rectangle2set(1, 2,15,11,18)
    f1['main_venue'] = rectangle2set(1, 2,19,11,28)
    f1['service'] = rectangle2set(1, 14,19,15,20)
    f1['room3'] = rectangle2set(1, 14,21,15,24)
    f1['room4'] = rectangle2set(1, 14,25,15,26)
    f1['toilet2'] = rectangle2set(1, 14,27,15,28)
    f1['lift1_f1'] = rectangle2set(1, 14, 10, 14, 11)
    f1['lift2_f1'] = rectangle2set(1, 1, 10, 1, 11)
    f1['entrance_f1'] = set([(1,13,0),(1,15,2),(1,15,4),(1,15,7)])
    f1['exit1_f1'] = set([(1,15,5),(1,15,15),(1,15,17),(1,0,19)])
    f1['space_f1'] = (rectangle2set(1, 12,1,14,28) | rectangle2set(1, 2,6,11,11) | rectangle2set(1, 1,12,1,28)) - (f1['register'] | f1['service'] | f1['room1'] | f1['room2'] | f1['poster_hall'] | f1['room3'] | f1['room4'] | f1['toilet2'] | f1['toilet1'] | f1['lift1_f1'])

    areas = {}

    for area in f1:
        sensors=f_pos2sensor(pos2sensor,f1[area])
        areas[area] = Area(floor=1, name=area, area=f1[area], sensor=sensors)
        for sen in sensors:
            sensor2area[sen] = area


    f2={}
    f2['diningroom'] = rectangle2set(2, 2,1,9,5)
    f2['room5'] = rectangle2set(2, 10,1,11,5)
    f2['relaxation_areas'] = rectangle2set(2, 13,0,15,5)
    f2['toilet3'] = rectangle2set(2, 4,10,5,11)
    f2['room6'] = rectangle2set(2, 6,10,7,11)
    f2['lift1_f2'] = rectangle2set(2, 14, 10, 14, 11)
    f2['lift2_f2'] = rectangle2set(2, 1, 10, 1, 11)
    f2['space_f2'] = rectangle2set(2, 2,6,14,9) | rectangle2set(2, 2,10,3,11) | rectangle2set(2, 12,10,13,11)

    for area in f2:
        sensors = f_pos2sensor(pos2sensor,f2[area])
        areas[area] = Area(floor=2, name=area, area=f2[area], sensor=sensors)
        for sen in sensors:
            sensor2area[sen] = area

    return areas, sensor2pos, sensor2area, pos2sensor
